package org.airdroid.scenemix;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class LevelView extends View {
	final static int ELEM_WIDTH = 20;
	final static int ELEM_HEIGHT = 10;
	
	int level = 0;
	Drawable green, red;
	
	public LevelView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public LevelView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public LevelView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		Resources r = context.getResources();
        green = r.getDrawable(R.drawable.level_green);
        red = r.getDrawable(R.drawable.level_red);

        setMinimumWidth(ELEM_WIDTH);
        setMinimumHeight(ELEM_HEIGHT * 23 / 2);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		int h = ELEM_HEIGHT * 23 / 2; // canvas.getHeight();
		
		for (int i = 0; i < 8 && i < this.level; i++) {
			Drawable d = (i < 5) ? green : red;
			d.setBounds(0, h - (i * ELEM_HEIGHT * 3 / 2 + ELEM_HEIGHT), ELEM_WIDTH, h - i * ELEM_HEIGHT * 3 / 2);
			d.draw(canvas);
		}
	}
	
	public void setLevel(int level) {
		this.level = level;
		invalidate();
	}
	
	public int getLevel() {
		return this.level;
	}
}
